<template>
  <div>
    <el-table :data="users" class="userTable" border>
      <el-table-column prop="username" label="用户名" width="width">
      </el-table-column>
      <el-table-column prop="enabled" label="状态" width="width">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enabled"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="role" label="角色" width="width">
      </el-table-column>
      <el-table-column prop="manager" label="直属领导" width="width">
      </el-table-column>
      <el-table-column label="操作" width="width">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleEnable(scope.$index, scope.row)"
            >启 用</el-button
          >
          <el-button
            type="danger"
            @click="handleDisable(scope.$index, scope.row)"
            >禁 用</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="createNewUser">
      <el-button type="primary" icon="el-icon-circle-plus" @click="openDialog"
        >新增用户</el-button
      >
      <el-dialog title="创建新用户" :visible.sync="dialogVisible" width="50%">
        <div>
          <el-form ref="form" :model="newUser" :rules="rules" label-width="100px">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="newUser.username" class="input" type="text"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="newUser.password" class="input" type="password"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="enabled">
              <el-radio-group v-model="newUser.enabled">
                <el-radio label="true">启用</el-radio>
                <el-radio label="false">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="角色" prop="role">
              <el-select
                v-model="newUser.role"
                placeholder="请选择角色"
                style="width: 50%"
              >
                <el-option label="职员" value="NORMAL"></el-option>
                <el-option label="经理" value="MANAGER"></el-option>
                <el-option label="用户管理" value="USERMANAGER"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="直属领导" prop="manager">
              <el-input v-model="newUser.manager" class="input" type="text"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-success"
                @click="handleSave"
                >请确认后提交</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newUser: {},
      users: [],
      username: "",
      dialogVisible: false,
      rules: {
        username: [{required: true, message:'请输入', trigger: 'blur'}],
        password: [{required: true, message:'请输入', trigger: 'blur'}],
        role:[{required: true, message:'请输入', trigger: 'blur'}],
        manager:[{required: true, message:'请输入', trigger: 'blur'}],
      },
    };
  },
  created() {
    this.axios
      .get("/user/userlist")
      .then((resp) => {
        this.users = resp.data;
      })
      .catch(() => {
        this.$message.error("未成功导入用户姓名");
      });
  },
  methods: {
    handleEnable(index, user) {
      this.users[index] = user;
      console.log(user.username);
      //   console.log(JSON.stringify() user);
      this.axios
        .get("/user/enableUser", { params: { username: user.username } })
        .then(() => {
          this.$message.success("启用当前用户！");
          user.enabled = true;
        });
    },
    handleDisable(index, user) {
      this.users[index] = user;
      this.axios
        .get("/user/disableUser", { params: { username: user.username } })
        .then(() => {
          this.$message.warning("禁用当前用户！");
          user.enabled = false;
        });
    },
    openDialog() {
      this.dialogVisible = true;
    },
    handleSave() {
        this.axios
        .post("/user/create", this.newUser)
        .then(() => {
            this.$message.success("创建用户成功");
        })
        .catch(() => {
            this.$message.error("创建用户失败");
        });
    },
  },
};
</script>

<style scoped>
.userTable {
  text-align: center;
  width: 100%;
}
.createNewUser {
  float: right;
  margin-top: 20px;
}
.input {
    width: 50%;
}
</style>